package com.xg.controller.boss;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xg.common.constant.CommonStatusEnum;
import com.xg.common.request.BrandReq;
import com.xg.common.result.ApiResult;
import com.xg.common.util.Func;
import com.xg.common.util.PageUtil;
import com.xg.common.util.PageUtilHelper;
import com.xg.entity.UsedCarBrand;
import com.xg.service.IUsedCarBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BeandController {

    @Autowired
    private IUsedCarBrandService brandService;

    @RequestMapping("/brand/list")
    ApiResult<List<UsedCarBrand>> findCarBrandList(){
        List<UsedCarBrand> list = brandService.list();
        return ApiResult.success(list);
    }

    @RequestMapping("/brand/queryPageList")
    ApiResult<PageUtil<UsedCarBrand>> queryPageList(@RequestBody BrandReq brandReq) {
        List<UsedCarBrand> list = brandService.list();
        if (!Func.isEmpty(brandReq.getBrandName())) {
            list = brandService.list(Wrappers.<UsedCarBrand>lambdaQuery().like(UsedCarBrand::getBrandName, brandReq.getBrandName()));
        }
        PageUtil<UsedCarBrand> pageUtil = PageUtilHelper.pageHelper(list, brandReq.getCpage(), brandReq.getPageSize());
        return ApiResult.success(pageUtil);
    }

    @RequestMapping("/brand/delete")
    ApiResult deleteBrands(@RequestParam("ids") List<Integer> ids) {
        boolean b = brandService.removeByIds(ids);
        if (b) {
            return ApiResult.success();
        } else {
            return ApiResult.error(CommonStatusEnum.BRAND_DELETE_ERROR.getCode(), CommonStatusEnum.BRAND_DELETE_ERROR.getMsg());
        }
    }

    @RequestMapping("/brand/info")
    ApiResult<UsedCarBrand> selectBrandInfo(@RequestParam("id") Integer bannerId) {
        UsedCarBrand brand = brandService.getOne(Wrappers.<UsedCarBrand>lambdaQuery().eq(UsedCarBrand::getBrandId, bannerId), false);
        if (!Func.isEmpty(brand)) {
            return ApiResult.success(brand);
        } else {
            return ApiResult.error(CommonStatusEnum.BRAND_INFO_ERROR.getCode(), CommonStatusEnum.BRAND_INFO_ERROR.getMsg());
        }
    }

    @RequestMapping("/brand/save")
    ApiResult saveBrand(@RequestBody UsedCarBrand usedCarBrand) {
        boolean b = false;
        if (!Func.isEmpty(usedCarBrand.getBrandId())) {
//            usedCarBrand.setUpdateTime(new Date());
            b = brandService.updateById(usedCarBrand);
        } else {
//            usedCarBrand.setCreateTime(new Date());
            b = brandService.save(usedCarBrand);
        }
        if (b) {
            return ApiResult.success();
        } else {
            return ApiResult.error(CommonStatusEnum.BRAND_SAVE_ERROR.getCode(), CommonStatusEnum.BRAND_SAVE_ERROR.getMsg());
        }
    }
}
